<?php
class User
{
    public $name="";
    public function setName($name)
    {
        $this->name = $name;
        return $this;
    }
    /** @param ChatRoom $group */
    public function sendGroupMessage($group, $message)
    {
        $group->push($this, $message);
    }
    public function receiveMessage($message)
    {
        echo "({$this->name})收到消息:$message";
    }
}
class ChatRoom
{
    /** @var User[] $users */
    public $users=[];
    public function addUser($user)
    {
        $this->users[] = $user;
    }
    public function push($sender, $message)
    {
        foreach ($this->users as $user) {
            if($user === $sender) continue;
            $user->receiveMessage($message);
        }
    }
}

$a = new User;
$a->setName("John");

$b = new User;
$b->setName("Robert");

$room = new ChatRoom;
$room->addUser($a);
$room->addUser($b);

$a->sendGroupMessage($room, "hello");
